package com.memory.admin.earn.controller;

import com.memory.common.utils.ResultVoUtil;
import com.memory.common.vo.ResultVo;
import com.memory.component.actionLog.annotation.EntityParam;
import com.memory.modules.business.domain.earn.CurDayPrice;
import com.memory.modules.business.domain.earn.DayEarn;
import com.memory.modules.business.service.earn.impl.DayEarnServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @PackageName: com.memory.admin.earnings.controller
 * @ClassName: EarningsController
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/10
 */
@Controller
@RequestMapping("/business/day/earn")
@RequiredArgsConstructor
public class DayEarnController {

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
    private final DayEarnServiceImpl dayEarnServiceImpl;

    @GetMapping("list")
    public String list(Model model, DayEarn dayEarn) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        Example<DayEarn> example = Example.of(dayEarn, exampleMatcher);
        final Page<DayEarn> page = dayEarnServiceImpl.findPage(example);
        model.addAttribute("list", page.getContent());
        model.addAttribute("page", page);
        return "/business/earn/dayearn/list";
    }

    @GetMapping("add")
    public String toAdd() {
        return "/business/earn/dayearn/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public ResultVo save(@EntityParam DayEarn dayEarn) {
        BigDecimal value = BigDecimal.ZERO;
        if(dayEarn.getValue1() != null) {
            value = value.add(dayEarn.getValue1());
        }
        if(dayEarn.getValue2() != null) {
            value = value.add(dayEarn.getValue2());
        }
        if(dayEarn.getValue3() != null) {
            value = value.add(dayEarn.getValue3());
        }
        if(dayEarn.getValue4() != null) {
            value = value.add(dayEarn.getValue4());
        }
        dayEarn.setTotalEarn(value);
        if(dayEarn.getId() != null) {
            dayEarnServiceImpl.modify(dayEarn.getId(),dayEarn);
        }else {
            DayEarn dayEarn1 = dayEarnServiceImpl.findByDate(dayEarn.getCreateDate());
            if(dayEarn1 == null) {
                dayEarnServiceImpl.create(dayEarn);
            }else {
                return ResultVoUtil.error("日期重复了");
            }
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultVo delete(@RequestParam(value = "ids", required = false) Long id){
        if (id != null){
            dayEarnServiceImpl.remove(id);
            return ResultVoUtil.success("删除成功");
        }

        return ResultVoUtil.error("请传入删除的id");
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") DayEarn dayEarn, Model model) {
        DayEarn dayEarn1 = dayEarnServiceImpl.findById(dayEarn.getId()).get();
        model.addAttribute("dayEarn", dayEarn1);
        return "/business/earn/dayearn/add";
    }

}
